﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using System.Collections;
using Microsoft.LightSwitch;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;

namespace LightSwitchApplication
{
    public class MovementsController : BasicController
    {
        // GET api/<controller>
        public object Get() //IEnumerable<string>
        {
            var result = (from m in DataWorkspace.MMC_Content.Movements.GetQuery().Execute()
                select new { Id = m.Id, Amount = m.Amount, CarId = (m.Car == null ? 0 : m.Car.Id)
                    , PlaceId = (m.Place == null ? 0 : m.Place.Id)
                    , DateFrom = m.DateFrom , DateTo = m.DateTo
                    , Description = m.Description
                });
            return result;
        }

        public object Get(int? PlaceId, int? CarId) //IEnumerable<string>
        {
            var result = (from m in DataWorkspace.MMC_Content.MovementsFiltered(PlaceId, CarId, DateTime.Now).Execute()
                select new { Id = m.Id, Amount = m.Amount, CarId = (m.Car == null ? 0 : m.Car.Id)
                    , PlaceId = (m.Place == null ? 0 : m.Place.Id)
                    , DateFrom = m.DateFrom , DateTo = m.DateTo
                    , Description = m.Description
                });
            return result;
        }

        // GET api/<controller>/5
        public object Get(int id)
        {
            return (from m in DataWorkspace.MMC_Content.Movements.GetQuery().Execute() where m.Id == id
                select new { Id = m.Id, Amount = m.Amount
                    , CarId = (m.Car == null ? 0 : m.Car.Id)
                    , PlaceId = (m.Place == null ? 0 : m.Place.Id)
                    , DateFrom = m.DateFrom
                    , DateTo = m.DateTo
                    , Description = m.Description
                }).FirstOrDefault();
        }

        //// POST api/<controller>
        //public void Post([FromBody]string value)
        //{
        //}

        //// PUT api/<controller>/5
        //public void Put(int id, [FromBody]string value)
        //{
        //}

        //// DELETE api/<controller>/5
        //public void Delete(int id)
        //{
        //    var Movement = DataWorkspace.MMC_Content.Movements_SingleOrDefault(id);
        //    if (Movement != null)
        //    {
        //        Movement.Delete();
        //        DataWorkspace.MMC_Content.SaveChanges();
        //    }
        //}
    }
}